dojo.require("dojo.data.ItemFileReadStore");

dojo.require("dijit.Tree");

dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.TabContainer");

dojo.require("dijit.form.Button");
dojo.require("dijit.form.Form");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.NumberTextBox");
dojo.require("dijit.form.Textarea");

dojo.require("dojox.data.JsonRestStore");
dojo.require("dojox.grid.DataGrid");

dojo.require("dojox.layout.GridContainerLite");

var deleteTrait = function(/* event */ e) {
	console.log("deleteTrait::entered.");
};

var clearTrait = function(/* event */ e) {
	console.log("clearTrait::entered.");
};

var resetTrait = function(/* event */ e) {
	console.log("refreshTrait::entered.");
};

var refreshStore = function() {
	var grid = dijit.byId("wt_Grid");
	grid.sort();
}

var saveTrait = function(/* event */ e) {
	console.log("saveTrait::entered.");
	var form = dijit.byId("wt_Form");
	var values = form.get("value");
	var grid = dijit.byId("wt_Grid");
	var store = grid.store;
	
	var selected = grid.selection.getSelected();
	if (selected.length > 0) {
		var obj = selected[0]; // using single selectionMode
		store.changing(obj);
		if (typeof(obj.label) != 'undefined') {
			store.setValue(obj, 'label', obj.label);
		}
		if (typeof obj.name != 'undefined') {
			store.setValue(obj, 'name', obj.name);
		}
		if (typeof obj.shortDesc != 'undefined') {
			store.setValue(obj, 'shortDesc', obj.shortDesc);
		}
		if (typeof obj.longDesc != 'undefined') {
			store.setValue(obj, 'longDesc', obj.longDesc);
		}
	} else {
		store.newItem(values);
	}
	
	store.save({onComplete: refreshStore})
};

var updateWeaponTab = function(/* event*/ e) {
	console.log("updateWeaponTab::entered.");
	var obj = e.grid.getItem(e.rowIndex);
	
	var values = {
		"label" : (typeof obj.label != 'undefined' ? obj.label : ""),  
		"name" : (typeof obj.name != 'undefined' ? obj.name : ""),
		"shortDesc" : (typeof obj.shortName != 'undefined' ? obj.shortDesc : ""),
		"longDesc" : (typeof obj.longName != 'undefined' ? obj.longDesc : ""),
	} 
	var form = dijit.byId("wt_Form");
	form.set('value', values);
}

var createWeaponTraitPane = function() {
	
	var pane = new dijit.layout.BorderContainer({
		gutters: false,
		title: "Weapon Traits",
		design: "sidebar"
	}, "wt_Tab");
	
    var traitStore = new dojox.data.JsonRestStore({
        target: "rest/WeaponTrait",
        idAttribute: "key",
        query : { WeaponTrait: "*" }
    });
    
    var gridLayout = [{
    	field: "label",
    	name: "Trait",
    	width: "116px"
    }];
        
    var grid = new dojox.grid.DataGrid({
    	structure: gridLayout,
    	store: traitStore,
    	region: "left",
    	selectionMode: "single",
    	sortInfo: -1,
    	style: "border: 1px solid blue; width: 120px;"
    }, "wt_Grid");    
    grid.setSortIndex(1, false);
    grid.sort();
    dojo.connect(grid, "onCellClick", updateWeaponTab);
        
    // Add the central dijits
    var centerPane = new dijit.layout.ContentPane({
    	region: "center",
    }, "wt_Pane");
    
    var labelBox = new dijit.form.TextBox({    	
    	name : "label",
    	maxLength: 20,  	    	
    }, "wt_Label");        
    var nameBox = new dijit.form.TextBox({    	
    	name : "name",
    	maxLength: 20,     	
    }, "wt_Name");
    var nameBox = new dijit.form.TextBox({    	
    	name : "shortDesc",
    	maxLength: 100,    	    	
    }, "wt_ShortName");    
    var nameBox = new dijit.form.TextBox({    	
    	name : "fullDesc",
    	maxLength: 255,	    	
    }, "wt_FullName");
    
    // Add the form
    var form = new dijit.form.Form({}, "wt_Form");
    form.startup();
    
    // Button panel
    // Delete, Clear, Reset, Save    
    var buttonPane = new dijit.layout.ContentPane({
    	splitter: false,
    	region: "bottom",
    }, "wt_ButtonPane");    
    
    var btn_Delete = new dijit.form.Button({
    	label : "Delete",
    }, "wt_Delete");
    // TODO: Need to add recursive check/update if this is deleted to adjust clients
    dojo.connect(btn_Delete, "onClick", deleteTrait);
    var btn_Clear = new dijit.form.Button({
    	label : "Clear",
    }, "wt_Clear");
    dojo.connect(btn_Clear, "onClick", clearTrait);
    var btn_Reset = new dijit.form.Button({
    	label : "Reset",
    }, "wt_Reset");
    dojo.connect(btn_Reset, "onClick", resetTrait);
    var btn_Save = new dijit.form.Button({
    	label : "Save",
    }, "wt_Save");
    dojo.connect(btn_Save, "onClick", saveTrait);

}

var doLayout = function() {
    var tc = new dijit.layout.TabContainer({
        style: "height: 100%; width: 100%; border: 1px solid black"
    }, "root_container");

    var cp = new dijit.layout.ContentPane({
        title: "Models",
        content: "Bar"
    }, "modelTab");
        
    cp = new dijit.layout.ContentPane({
        title: "Weapons",
        content: "Foo"
    }, "weaponTab");
   
    cp = new dijit.layout.ContentPane({
        title: "Factions",
        content: "Foo"
    }, "factionTab");
    
    cp = new dijit.layout.ContentPane({
        title: "Model traits",
        content: "Foo"
    }, "modelTraitTab");

    createWeaponTraitPane();
    
    tc.startup();
}

dojo.addOnLoad(doLayout);